package org.jwebuilder.asm;

import java.io.IOException;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.commons.SerialVersionUIDAdder;

public class SerializableClassLoader extends ClassLoader
{
	public SerializableClassLoader()
	{
		
	}

	public SerializableClassLoader(ClassLoader parent)
	{
		super(parent);
	}

	@Override
	protected Class<?> findClass(String name) throws ClassNotFoundException
	{
		ClassReader classReader = null;
		try
		{
			classReader = new ClassReader(name);
		}
		catch (IOException e)
		{
			throw new RuntimeException(e);
		}
		ClassWriter classWriter = new ClassWriter(classReader, 0);
		SerialVersionUIDAdder serialVersionUIDAdder = new SerialVersionUIDAdder(classWriter);
		SerializableClassAdapter serializableClassAdapter = new SerializableClassAdapter(serialVersionUIDAdder);
		classReader.accept(serializableClassAdapter, 0);
		byte[] b = classWriter.toByteArray();
		return defineClass(name, b, 0, b.length);
	}
}